package rfc3961// Implementation of the n-fold algorithm as defined in RFC 3961./* CreditsThis golang implementation of nfold used the following project for help with implementation detail.Although their source is in java it was helpful as a reference implementation of the RFC.You can find the source code of their open source project along with license information below.We acknowledge and are grateful to these developers for their contributions to open sourceProject: Apache Directory (http://http://directory.apache.org/)https://svn.apache.org/repos/asf/directory/apacheds/tags/1.5.1/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/crypto/encryption/NFold.javaLicense: http://www.apache.org/licenses/LICENSE-2.0*/// Nfold expands the key to ensure it is not smaller than one cipher block.// Defined in RFC 3961.//// m input bytes that will be "stretched" to the least common multiple of n bits and the bit length of m.func ( []byte, int) []byte { := len() * 8//Get the lowest common multiple of the two bit sizes := lcm(, ) := / var []bytefor := 0; < ; ++ { := 13 * = append(, rotateRight(, )...) } := make([]byte, /8) := make([]byte, /8)for := 0; < /; ++ {for := 0; < /8; ++ { [] = [+(*len())] } = onesComplementAddition(, ) }return}func onesComplementAddition(, []byte) []byte { := len() * 8 := make([]byte, /8) := 0for := - 1; > -1; -- { := getBit(&, ) := getBit(&, ) := + + if == 0 || == 1 {setBit(&, , ) = 0 } elseif == 2 { = 1 } elseif == 3 {setBit(&, , 1) = 1 } }if == 1 { := make([]byte, len()) [len()-1] = 1 = (, ) }return}func rotateRight( []byte, int) []byte { := make([]byte, len()) := len() * 8for := 0; < ; ++ { := getBit(&, )setBit(&, (+)%, ) }return}func lcm(, int) int {return ( * ) / gcd(, )}func gcd(, int) int {for != 0 { , = , % }return}func getBit( *[]byte, int) int { := / 8 := uint( % 8) := (*)[] := int( >> (8 - ( + 1)) & 0x0001)return}func setBit( *[]byte, , int) { := / 8 := uint( % 8) := (*)[]varbyte = byte(<<(8-(+1))) | (*)[] = }
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.