Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:06:04+00:00 2026-05-25T15:06:04+00:00

— code changed — Suppose i have an interface and two classes, one of

  • 0

— code changed —

Suppose i have an interface and two classes, one of which extends the other one:

interface EncryptionModule {
    public function setup();

    public function encrypt($originalString);

    public function decrypt($encryptedString);
}

class GeneralOneWayHashingModule {
    public static $encryptionType = ALG_TYPE_HASH_FUNCTION;
    protected $salt;

    public function setSalt($salt) {
        $this->salt = $salt;
    }


    public function decrypt($encryptedString) {
        throw new Exception(__CLASS__ . ' ' . __LINE__ . ': cannot reverse an hash function ');
    }
}

class BCryptHashingModule extends GeneralOneWayHashingModule implements GeneralOneWayHashingModule {
    protected $cost;

    public function setup() {
        $this->cost = 10;
    }

    public function setCost($cost) {
        $this->cost = $cost;
    }

    public function encrypt($originalString) {
        return crypt($originalString, '$2a$' . str_pad($this->cost, 2, '0', STR_PAD_LEFT) . '$' . $this->salt);
    }
}

I wonder which one of the two classes should implement EncryptionModule interface.. the extending one (this is my choice)? Both classes, maybe with GeneralOneWayHashingModule declared as abstract and implementing all functions declared in EncryptionModule? Only the extended one?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:06:05+00:00Added an answer on May 25, 2026 at 3:06 pm

    The question whether GeneralOneWayHashingModule should be abstract is unrelated to the other question: whether it should implement this interface. And the answer to both is: It depends on the Responsibilities of the classes, whether it is intended to be instantiated, and possibly other decisions.

    I could give a better answer if I knew what each class is for and which methods it has.

    EDIT after code-update

    I see several issues here:

    • Why do you call the class …Module? I do not think it is a module, but a regular class. Its responsibility is encrypting stuff. I would rather call it Encryption (Names are extremely important to me, they will tell me what this class is for, even if I’m concentrating on something else – which is usually the case when I’m using a class)
    • As Carlos points out correctly in a comment to your question Encryption != Hashing and Encryption is by definition reverseable, whereas hashing is not, the two classes should not be extending each other.
    • What is the purpose of the setup() function? I think you added that function because you noticed that some sub-class needs it for its implementation. The question should rather be: Do all encryption functions have a set-up routine? Is it mandatory? I do not think so.
    • Another problem you have is that you want some of your encryption routines to use salt, whereas others do not seem to need one. Since salt is usually not re-used (i.e. you usually have a new salt for each encryption), it should not be a class member, otherwise your class has a state which is not what you want (i.e. $encryption can no longer encrypt() 2 strings in a row, its state has to be changed – setSalt() has to be called – for it to be able to encrypt the second string).
    • But the real question is: What do you need this class structure for? For testing/showcasing different algorithms or for storing/verifying passwords? If you just want to implement password verification, you are over-engineering, you could rather use something like this:
      class User {
          public function encryptPassword($password, $salt) {
              // …
          }
          public function verifyPassword($password, $salt, $encryptedPassword);
              return $encryptedPassword == self::encryptPassword($password, $salt);
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
— or — Is there a difference between these? Is one better-supported than the
I need to stack two tables on top of each other with content before
All of my code is on my SVN, but I want some of it
So to start, I have an array of XML files. These files need to
Is it possible to nest forms in Wicket that are independent of each other?
Any idea whats the difference between the two commands below? Command: manage.py runfcgi method=threaded
This is what I'm doing: public static String htmlToText(String inString) { String noentity=StringEscapeUtils.unescapeHtml(inString); return
We have different types of hyphens/dashes (in some text) populated in db. Before comparing
I have a fairly mundane piece jQuery that toggles a div's visibility when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.